#include "translate.h"

translate::translate()
{
    for(int i=0;i<8;i++)
    {
       bin_file.magic[i]=HEADER_MAGIC[i];
    }
    bin_file.flag=26;
}

void translate::convert_bin(const MSTAR_TV *ms_tv)
{
   // ms_tv->Read_ATV();
   // qint num=ms_tv
    Q_CHECK_PTR(ms_tv);
    this->bin_file.dtv_atv_length=(qint32)ms_tv->count_atv;
    QFile file(STD_FILE_FR_TV);
    if(!file.open(QFile::WriteOnly))
    {
         qDebug("in translate::convert_bin open file error");
         return ;
    }

    QDataStream out(&file);
    const char *tmp=(const char *)bin_file.magic;
    out<<tmp
            <<bin_file.flag
            <<bin_file.dtv_atv_length;

    for(uint i=0;i<MAX_NUMBER_OF_ATV_PROGRAM;i++)
    {
        ushort val=(ushort)ms_tv->mstar_atv->ATVProgramData[i].misc.u_ChannelNumber;
        out<<val;
        const char *str=(const char *)ms_tv->mstar_atv->ATVProgramData[i].ui_sName;
        QString q_str(str);
        out<<q_str;
        val=65;//A
        out<<val;
        val=(ushort)ms_tv->mstar_atv->ATVProgramData[i].misc.u_Skip;
        out<<val;
        val=(ushort)ms_tv->mstar_atv->ATVProgramData[i].misc.u_Favorite;
        out<<val;


    }
   file.flush();
    MSTAR_DTV_DATA *dtv_data;
    for(int i=0;i<ms_tv->mstar_dtv.size();i++)
    {
        dtv_data=ms_tv->mstar_dtv.at(i);
        Q_CHECK_PTR(dtv_data);
       /* if(!dtv_data)
        {
                qDebug("in translate::convert_bin call ms_tv->mstar_dtv.at(i) is NULL,error");
                return ;
        }*/
        ushort val=dtv_data->ui2_Number;
        out<<val;

        const char *str=(const char *)dtv_data->ui_ServiceName;
        QString name(str);
        out<<name;

        if(i<ms_tv->count_dtvb)
            val=(ushort)66;
        else
            val=(ushort)84;
        out<<val;
        val=(ushort)dtv_data->ui_Favorite;
        out<<val;
        val=(ushort)dtv_data->u_IsSkipped;
        out<<val;
       file.flush();
    }
  file.close();

}


//根据标准数据结构的修改来更新tv端数据结构的修改

void translate::bin_modify(MSTAR_TV *ms_tv)
{

    Q_CHECK_PTR(ms_tv);
    //重写文件
    ms_tv->Write_ATV();
    ms_tv->Write_DTV();

    if(remove("database/dtv_cmdb_0.bin")!=0)
            perror("remove file dtv_cmdb_0 error\n");
    remove("database/dtv_cmdb_1.bin");
    rename("database/cmdb_0_tmp.bin","database/dtv_cmdb_0.bin");
    rename("database/cmdb_1_tmp.bin","database/dtv_cmdb_1.bin");
    remove("database/cmdb_0_tmp.bin");
    remove("database/cmdb_1_tmp.bin");
    //QFile oldfile("stand")


}
